<?php

include_once "quickcrud_connect.php";
include_once "quickcrud_common.php";
include "CF_mysql_com.php";
include_once "CF_all_com.php";

if (!isset($_REQUEST['crudset_id']) OR !isset($_REQUEST['project_id']) ){
	print 'Please select or create a project and set to use first';
	exit;
}

	$project_id = $_REQUEST['project_id'];
	$crudset_id = $_REQUEST['crudset_id'];


	$qry_project_info = "SELECT * FROM projects WHERE project_id = $project_id";

	$project_info = get_array_query($qry_project_info);
	
	$project_db = $project_info[1]['project_db'];
	$project_path = $project_info[1]['project_path'];
	$project_server = $project_info[1]['project_server'];
	$project_login = $project_info[1]['project_login'];
	$project_pass = $project_info[1]['project_pass'];
	$project_name = $project_info[1]['project_name'];
    $class_subpath = $project_info[1]['class_subpath'];
	$template_dir = $project_info[1]['template_dir'];
	
    if(!empty($class_subpath)){
        $class_subpath = $class_subpath . '/';
        }


	$qry_set_info = "SELECT * FROM crudsets 
            JOIN tablesources ON tablesources.tablesource_id = crudsets.main_table 
    WHERE crudset_id = $crudset_id";

	$set_info = get_array_query($qry_set_info);

// fields from database
	$cs_name = $set_info[1]['cs_name'];
	$paging = $set_info[1]['paging'];
    $filter = $set_info[1]['filter'];
    $sort = $set_info[1]['sort'];
	$query = $set_info[1]['query'];
	$table_name = $set_info[1]['table_name'];
	$cs_type = $set_info[1]['cs_type'];
    $class_base_name = $set_info[1]['class_base_name'];
    $class_name = $set_info[1]['class_name'];
    $html_table_name = $set_info[1]['html_table_name'];
    $list_table_instance = $set_info[1]['list_table_instance'];
    $paging = $set_info[1]['paging'];
	$multiact = $set_info[1]['multiact'];
	$css_file = $set_info[1]['css_file'];	
    //$paging = $set_info[1]['paging'];
    //$paging = $set_info[1]['paging'];
	$subdirectory = $set_info[1]['subdirectory'];
	$needs_login = $set_info[1]['needs_login'];

// calculated fields
    $process_page = $cs_name . '_proc.php';
	$edit_page = $cs_name . '_edit.php';
    $list_page = $cs_name . '_list.php';
    if($paging == 1){
        $no_paging = '';
		$yes_paging = '//';
        }
     else{
        $no_paging = '//';
		$yes_paging = '';
        }
    if($sort == 1){
        $no_sort = '';
        $header_type = 'make_sort_heading';
        }
        $no_sort = '';
        $header_type = 'make_sort_heading';
     //else{
     //   $no_sort = '//';
     //   $header_type = 'make_simple_heading';
     //   }
    if($filter == 1){
        $no_filter = '';
        }
     else{
        $no_filter = '//';
        }

		
	if($multiact == 1){
		$no_multiact = '';
		$yes_multiact = '//';
		
	}else{
		$no_multiact = '//';
		$yes_multiact = '';
	}

// Calcuate back directory $backdirectory from levels in subdirectory
	$subdirlevels = substr_count($subdirectory,'/');
	while ($subdirlevels > 0){
		$backdirectory .= "../";
		$subdirlevels --;
	}		
// comment out login logic or not
	if($needs_login == 1){
		$no_login = '';
	}
	else{
		$no_login = '//';
	}	
// if subdirectory path not exists, create
if(!empty($subdirectory)){
	$subdirname = rtrim($subdirectory, "/");
		if(!file_exists('projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirname))
		{
			mkdir('projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirname);
		}
	}
		
// Active /inactivate



	$qry_get_keys = "SELECT variable_name FROM crudcols 
	JOIN variables ON variables.variable_id = crudcols.variable_id 
	WHERE crudset_id = $crudset_id AND crudcols.is_key = 1";
	
	$firstkey = true;
	$keys_info = get_array_query($qry_get_keys);
    if (is_array($keys_info)){
	foreach ($keys_info AS $key){
		if($firstkey){
			$str_key = $key['variable_name'];
			$firstkey = false;
		}
		else{
			$str_key .= "," . $key['variable_name'];
		}
	}
	}

	$str_row_limit = ". \" \$" . $list_table_instance . "->post_rowlimit\"";
	$str_qry_sort = "  \$" . $list_table_instance . "->make_query_sort()";
	$str_qry_filter = " \$" . $list_table_instance . "->make_query_filter()";
	
	

	$str_query = "SELECT * FROM $table_name WHERE 1 ";
	
	// If passed a template file (tplfile), use it.  Else, use the default
	if(isset($_REQUEST['tplfile'])){
		$tplfile = $template_dir . $_REQUEST['tplfile'];
	}else{
		$tplfile = $template_dir . 'mformTPL.php';
	}	
	$str_list_file = file_get_contents($tplfile);

    $str_list_file = str_replace("%class_subpath%",$class_subpath,$str_list_file);
    $str_list_file = str_replace("%subdirectory%",$subdirectory,$str_list_file);
    $str_list_file = str_replace("%backdirectory%",$backdirectory,$str_list_file);
    $str_list_file = str_replace("%no_login%",$no_login,$str_list_file);
	
	$str_list_file = str_replace("%str_row_limit%",$str_row_limit,$str_list_file);	
	$str_list_file = str_replace("%str_qry_filter%",$str_qry_filter,$str_list_file);	
	$str_list_file = str_replace("%str_qry_sort%",$str_qry_sort,$str_list_file);	
	$str_list_file = str_replace("%str_query%",$str_query,$str_list_file);	
	$str_list_file = str_replace("%process_page%",$process_page,$str_list_file);
	$str_list_file = str_replace("%edit_page%",$edit_page,$str_list_file);
	$str_list_file = str_replace("%list_page%",$list_page,$str_list_file);
	$str_list_file = str_replace("%class_name%",$class_name,$str_list_file);
	$str_list_file = str_replace("%class_base_name%",$class_base_name,$str_list_file);
	$str_list_file = str_replace("%html_table_name%",$html_table_name,$str_list_file);
	$str_list_file = str_replace("%cs_name%",$cs_name,$str_list_file);
	$str_list_file = str_replace("%header_type%",$header_type,$str_list_file);
	$str_list_file = str_replace("%css_file%",$css_file,$str_list_file);	


	$str_list_file = str_replace("%connectinclude%",$project_name . "_connect.php",$str_list_file);
	$str_list_file = str_replace("%fieldinclude%",$cs_name . "_fields.php",$str_list_file);
	$str_list_file = str_replace("%cs_name%",$cs_name,$str_list_file);
	$str_list_file = str_replace("%str_key%",$str_key,$str_list_file);
	$str_list_file = str_replace("%no_paging%",$no_paging,$str_list_file);
	$str_list_file = str_replace("%yes_paging%",$yes_paging,$str_list_file);
	$str_list_file = str_replace("%no_sort%",$no_sort,$str_list_file);
	$str_list_file = str_replace("%no_filter%",$no_filter,$str_list_file);
 	$str_list_file = str_replace("%no_multiact%",$no_multiact,$str_list_file);         
 	$str_list_file = str_replace("%yes_multiact%",$yes_multiact,$str_list_file); 	

    $str_list_file = str_replace("%list_table_instance%",$list_table_instance,$str_list_file);


if($_REQUEST['output'] == 'screen')	{
    	echo "<html>\n<head>\n</head>\n<body>";
        echo "<TEXTAREA NAME=\"comments\" COLS='80' ROWS='20'>$str_list_file</TEXTAREA>\n";
		echo "<p>Make sure the appropriate supporing files and css are in the directory</p>\n";		
        echo "</body></html>";


    }else{
		// if passed an outfile, write to it.  Else, use the default
		if(isset($_REQUEST['outfile'])){
			$outfile = 'projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirectory . $_REQUEST['outfile'];
		}else{
			$outfile = 'projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirectory . $cs_name . "_list.php";
		}
		file_put_contents($outfile,$str_list_file);	
        //file_put_contents('projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirectory . $cs_name . "_list.php",$str_list_file);
		
		// check for css file, and write it if it is not there
		if(!empty($class_subpath)){
			$classlib_path = $project_path . '/' . $class_subpath;
		}else{
			$classlib_path = $project_path;
		}
		$check_css_file = "projects/" . $classlib_path  .$css_file;
		$source_css = "CFclass/css/" . $css_file;
		if(!file_exists($check_css_file))
			{
				copy($source_css,$check_css_file);
			}		
		
		
        $result = "Wrote List Page To $output_file";
        $result = urlencode($result);

        $header_text = "Location: leftmenu.php?project_id=$project_id&crudset_id=$crudset_id&result=$result";
        header($header_text); /* Redirect browser */
        exit;
	}

	

?>